<?php
			ob_start();			
?>
<?php
		
		if (isset($_COOKIE["user"])) {
			// if cookie <> ""
		  }
		  else
		  {
			  header("location:login.php");
			  
		  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/form_css.css" rel="stylesheet" type="text/css"  />
</head>
<?php
function updatePublish($publish)
{	
	$publish = $publish == 1 ? 0 : 1;
	$id_pub = $_REQUEST["id_pub"];
	$sql = "UPDATE tbl_category SET public={$publish} WHERE cat_id=$id_pub";
	query($sql);	
}
?>
<?php
if(isset($_REQUEST["public"]))
{
	updatePublish($_REQUEST["public"]);
	
}
?>
<?php
function deleteRecord($id_imagename)
{
	//$cat_id=$_REQUEST["cat_id"];
	//get id from array
	$cat_id = key($id_imagename);
	//get cat_image from array
	$imagename = current($id_imagename);
	
	$sql = "DELETE FROM tbl_category WHERE cat_id={$cat_id}";
	$result = query($sql);	
	if(query($sql))
	{
		//delete image category
		deleteFile("category/{$imagename}");
		
		//*** Loop Delete product ***//
		$strSQL2 = "SELECT * FROM tbl_product WHERE cat_id = '".$cat_id."' ORDER BY pro_id DESC ";
		$objQuery2 = mysql_query($strSQL2) or die ("Error Query [".$strSQL2."]");
		while($objResult2 = mysql_fetch_array($objQuery2))
		{
			if($objQuery2)
			//*** Delete image product (product) ***//			
			deleteFile("product/".$objResult2["photo"]);
			deleteFile("product/".$objResult2["photo"]."-d.jpg");
		}
		
		//*** Delete All Rows  (tbl_product)***//
		$strSQL = " DELETE FROM tbl_product ";
		$strSQL .=" WHERE cat_id = '".$cat_id."' ";
		$objQuery = mysql_query($strSQL);		

		
		//end delete all product
		errorMessage("Supprimer réussies!");
	}	
	else
	{
		errorMessage("Supprimer échec ..!");
	}	
}
/*if(isset($_REQUEST["cat_id"]))
{
	deleteRecord($_REQUEST["cat_id"]);
}*/
?>

<?php
	if(@is_array($_REQUEST["cat_id"]))
{
	$id_imagename = $_REQUEST["cat_id"];
	deleteRecord($id_imagename);
}
?>

<?php
		if(isset($_REQUEST["orders"]))
		{
			$order = getParameter("post","orders");
			foreach ($order as $k=>$v){
				$v = intval($v);
				$sql = "UPDATE tbl_category SET orders=".$v." WHERE cat_id=".$k;
				$result=mysql_query($sql);
			}
			if($result)
				{
					errorMessage("Mise à jour réussie pour ...!");
				}else
				{
					errorMessage("Erreur de commande mis à jour ...!");
				}
		}
?>
<body>
                        <div id="border_cont_title">
                        	<div id="content_title_left">GESTION DE CATÉGORIE</div>
                            <div id="content_title_right"><a href="index.php?page=categoryNew">nouvelle catégorie</a></div>
                        </div>
                           <?php echo @$errors;?>
                    	<!--start table -->
                            <table width="770px">
                            <form action="index.php?page=category" method="post" name=frmcategory >
                            	<tr>
                                    <th>Photo</th>
                                	<th>Catégorie</th>
                                    <th>Public</th>
                                    <th>
                                    Ordre
                                      <input type="image"  src="images/b_save.png" name="btnorder" width="16" height="16"   />
                                    </th>
                                    <th>Action</th>
                                </tr>
                                <?php 
                                    $sql = "SELECT * from  tbl_category"; 
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                ?>
                                <tr>
                                	<td>
                                    	<img src="category/<?php echo $row["photo"];?>"  width="100" height="80" />
                                    </td>
                                	<td>
                                    	<?php echo $row["cat_name"];?>
                                    </td>
                                    <td align="center">
                                    	<?php 
                                            $icon_publish = $row["public"]==0 ? "publish_x.png" : "publish_y.png";
                                            $publish_image = "<img width='15px' src='images/{$icon_publish}' />";														
                                        ?>
                                            <a href="index.php?page=category&public=<?php echo $row["public"] . "&id_pub=" . $row["cat_id"];?>">
                                            <?php echo $publish_image; ?></a>
                                    </td>
                                    <td align="center">
                                        <input type="text" value="<?php echo $row["orders"];?>" name="orders[<?php echo $row["cat_id"];?>]" size="1" />
                                       
                                    </td>
                                    <td width="100" align="center">
                                   <a href="index.php?page=categoryEdit&cat_id=<?php echo $row["cat_id"]?>"><img class="action_distant" src="images/edit.png"  /></a>
                                   <a href="index.php?page=category&cat_id[<?php echo $row["cat_id"];?>]=<?php echo $row["photo"];?>" onclick="return confirm('Êtes-vous sûr de vouloir supprimer cette catégorie? Aussi tous les produits supprimera tous ....!');"><img class="action_distant" src="images/delete.png" /></a>
                                    </td>
                                	
                                </tr>
                                <?php
									}//end while
                                ?>
                              </form>   
                            </table>
                         <!--end table -->
							
</body>
</html>
